home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
xvisrc.zip
/
TERMCAP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-28
|
26KB
|
1,139 lines
/* Copyright (c) 1990,1991,1992 Chris and John Downey */
#ifndef lint
static char *sccsid = "@(#)termcap.c 2.1 (Chris & John Downey) 7/29/92";
#endif
/***
* program name:
xvi
* function:
PD version of UNIX "vi" editor, with extensions.
* module name:
termcap.c
* module function:
Termcap terminal interface module.
The following capabilities are not yet used, but may be added in future
to improve efficiency (at the expense of code compactness):
cr str Carriage return, (default ^M)
dC num Number of milliseconds of cr delay needed
nc bool No correctly working carriage return (DM2500,H2000)
xr bool Return acts like ce \r \n (Delta Data)
ch str Like cm but horizontal motion only, line stays same
DO str down N lines
up str Upline (cursor up)
UP str up N lines
LE str left N chars
RI str right N spaces
ll str Last line, first column
sc str save cursor
rc str restore cursor from last "sc"
dB num Number of milliseconds of bs delay needed
AL str add N new blank lines
DL str delete N lines
dc str Delete character
dm str Delete mode (enter)
ed str End delete mode
ip str Insert pad after character inserted
in bool Insert mode distinguishes nulls on display
mi bool Safe to move while in insert mode
dF num Number of milliseconds of ff delay needed
cd str Clear to end of display
xs bool Standout not erased by writing over it (HP 264?)
ms bool Safe to move while in standout and underline mode
* history:
STEVIE - ST Editor for VI Enthusiasts, Version 3.10
Originally by Tim Thompson (twitch!tjt)
Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
Heavily modified by Chris & John Downey
***/
#include "xvi.h"
/* #define SG_TEST 1 */
static void xyupdate P((void));
static void fail P((char *));
static void set_scroll_region P((int, int));
/*
* These are used to optimise output - they hold the current
* "real" and "virtual" coordinates of the cursor, i.e. where
* it is and where it is supposed to be.
*
* The "optimise" variable says whether we should cache cursor
* positions or should just go to the place asked; it is
* unset at the start so that the first goto sets things up.
*
* Note that the functions "outchar" and "outstr" should be
* used for strings of ordinary characters; stdio primitives
* are used internally to put out escape sequences.
*/
static int real_row = 0, real_col = 0;
static int virt_row = 0, virt_col = 0;
static bool_t optimise = FALSE;
/*
* Termcap-related declarations.
*/
extern char *tgetstr();
extern char *tgoto();
/*
* Exported.
*/
int cost_goto = 0; /* cost of doing a goto */
bool_t can_scroll_area = FALSE; /* true if we can set scroll region */
bool_t can_del_line; /* true if we can delete lines */
bool_t can_ins_line; /* true if we can insert lines */
bool_t can_inschar; /* true if we can insert characters */
unsigned int CO = 0; /* screen dimensions; 0 at start */
unsigned int LI = 0;
/*
* Needed by termcap library.
*/
char PC; /* pad character */
/*
* Internal string, num and boolean defs.
*/
static char *KS, *KE; /* keypad transmit start/end */
static char *VS, *VE; /* visual start/end */
static char *TI, *TE; /* cursor motion start/end */
static char *CE, *CL; /* erase line/display */
static char *AL, *DL; /* insert/delete line */
static char *IC, *IM, *EI; /* insert character / insert mode */
static char *CM; /* cursor motion string */
static char *HO; /* cursor to home position */
static char *CS; /* change scroll region */
static char *sf, *sr; /* scroll forward/reverse 1 line */
static char *SF, *SR; /* scroll forward/reverse n lines */
static char *SO, *SE; /* standout mode start/end */
static char *VB; /* visual bell */
static char *colours[10]; /* colour caps c0 .. c9 */
static int ncolours; /* number of colour caps we have */
static char BC; /* backspace char */
static char ND; /* backspace char */
static char DO; /* down one line */
static bool_t can_backspace; /* true if can backspace (bs/bc) */
static bool_t can_fwdspace = FALSE; /* true if can forward space (nd) */
static bool_t can_movedown = FALSE; /* true if can move down (do) */
static bool_t auto_margins; /* true if AM is set */
/*
* We use this table to perform mappings from cursor keys
* into appropriate xvi input keys (in command mode).
*/
static char arrow_keys[] = {
K_UARROW, '\0',
K_DARROW, '\0',
K_RARROW, '\0',
K_LARROW, '\0',
CTRL('B'), '\0',
CTRL('F'), '\0',
K_HELP, '\0',
};
static struct {
char *key_tcname;
char *key_rhs;
} keys[] = {
"ku", arrow_keys + 0, /* up */
"kd", arrow_keys + 2, /* down */
"kr", arrow_keys + 4, /* right */
"kl", arrow_keys + 6, /* left */
"kP", arrow_keys + 8, /* page up */
"kN", arrow_keys + 10,/* page down */
"kh", "H", /* home */
"k0", "#0", /* function key 0 */
"k1", arrow_keys + 12,/* help */
"k2", "#2", /* function key 2 */
"k3", "#3", /* function key 3 */
"k4", "#4", /* function key 4 */
"k5", "#5", /* function key 5 */
"k6", "#6", /* function key 6 */
"k7", "#7", /* function key 7 */
"k8", "#8", /* function key 8 */
"k9", "#9", /* function key 9 */
NULL
};
/*
* Standout glitch: number of spaces left when entering or leaving
* standout mode.
*
* This abomination is still needed for some terminals (e.g.
* Televideo).
*/
int SG;
/*
* Used by scroll region optimisation.
*/
static int s_top = 0, s_bottom = 0;
/*
* Used for colour-setting optimisation.
*/
#define NO_COLOUR -1
static int old_colour = NO_COLOUR;
/*
* Flush any pending output, including cursor position.
*/
void
flush_output()
{
xyupdate();
oflush();
}
/*
* Put out a "normal" character, updating the cursor position.
*/
void
outchar(c)
register int c;
{
xyupdate();
real_col++;
virt_col++;
if (real_col >= CO) {
if (auto_margins) {
virt_col = (real_col = 0);
virt_row = (real_row += 1);
} else {
optimise = FALSE;
}
}
moutch(c);
}
/*
* Put out a "normal" string, updating the cursor position.
*/
void
outstr(s)
register char *s;
{
xyupdate();
while (*s != '\0') {
real_col++;
virt_col++;
moutch(*s++);
}
/*
* We only worry about whether we have hit the right-hand margin
* at the end of the string; this is okay so long as we can trust
* the calling code not to use outstr if the string is going to
* wrap around.
*/
if (real_col >= CO) {
if (auto_margins) {
virt_col = (real_col %= CO);
virt_row = (real_row += 1);
} else {
optimise = FALSE;
}
}
}
/*
* This routine is called by tty_open() if for some reason the terminal
* is unsuitable.
*/
static void
fail(str)
char *str;
{
/*
* Assume we are in raw mode already, so set back into cooked.
*/
sys_endv();
(void) fputs(str, stderr);
putc('\n', stderr);
exit(2);
}
/*
* Look up term entry in termcap database, and set up all the strings.
*/
void
tty_open(prows, pcolumns)
unsigned int *prows;
unsigned int *pcolumns;
{
char tcbuf[1024]; /* buffer for termcap entry */
char *termtype; /* terminal type */
static char strings[512]; /* space for storing strings */
char *strp = strings; /* ptr to space left in strings */
char *cp; /* temp for single char strings */
int i;
termtype = getenv("TERM");
if (termtype == NULL) {
fail("Can't find your terminal type.");
}
switch (tgetent(tcbuf, termtype)) {
case -1:
fail("Can't open termcap.");
/*NOTREACHED*/
case 0:
fail("Can't find entry for your terminal in termcap.");
/*NOTREACHED*/
}
/*
* Booleans.
*/
auto_margins = (bool_t) (tgetflag("am") && !tgetflag("xn"));
can_backspace = (bool_t) tgetflag("bs");
/*
* Integers.
*/
/*
* Screen dimensions. Ask termcap for its values if we haven't
* already got any.
*/
if (*pcolumns == 0) {
int iv;
iv = tgetnum("co");
if (iv <= 0) {
fail("`co' entry in termcap is invalid or missing.");
}
*pcolumns = CO = (unsigned) iv;
} else {
CO = *pcolumns;
}
if (*prows ==